package com.geotmt.billingcenter.modules.consume.mapper.provider;

import com.alibaba.druid.util.StringUtils;
import com.geotmt.billingcenter.common.vo.params.ParamsConsume;
import org.apache.ibatis.annotations.Param;

/**
 * @Description 条件Sql
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/14 10:08
 * @Version     v1
 **/
public abstract class WhereProvider {

    /**
     * 消耗的 where 语句
     * @param sql
     * @param obj
     */
    public void whereConsume(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsConsume obj){
        sql.append(" AND ").append(baseAlias).append(".status <>2");
        if(!StringUtils.isEmpty(obj.getProductCode())){
            sql.append(" AND ").append(baseAlias).append(".product_code=#{productCode}");
        }
        if(null != obj.getCompanyId()){
            sql.append(" AND ").append(baseAlias).append(".company_id=#{companyId}");
        }
        if(null != obj.getCuserId()){
            sql.append(" AND ").append(baseAlias).append(".cuser_id=#{cuserId}");
        }
        if(null != obj.getInterfaceId()){
            sql.append(" AND ").append(baseAlias).append(".interface_id=#{interfaceId}");
        }
        if(null != obj.getFeeFlag()){
            sql.append(" AND ").append(baseAlias).append(".fee_flag=#{feeFlag}");
        }
        String interfaceIds = obj.getInterfaceIds();
        if(!StringUtils.isEmpty(interfaceIds)){
            String ids = interfaceIds.substring(0, interfaceIds.length() - 1);
            //写死的 接口表别名
            sql.append(" AND ").append("inter").append(".id IN(").append(ids).append(")");
        }
        if("currHis".equals(baseAlias)){
            if(null != obj.getEffectStartTime() && null != obj.getEffectEndTime()){
                sql.append(" AND (")
                        .append("(#{effectStartTime}>=").append(baseAlias).append(".effect_start_time AND #{effectStartTime} <=").append(baseAlias).append(".effect_end_time)")
                        .append("OR (#{effectEndTime}>=").append(baseAlias).append(".effect_start_time AND #{effectEndTime} <=").append(baseAlias).append(".effect_end_time)")
                        .append("OR (#{effectStartTime}<=").append(baseAlias).append(".effect_start_time AND #{effectEndTime}>= ").append(baseAlias).append(".effect_end_time)")
                        .append(")");
            }
        }else{
            if(null != obj.getEffectStartTime() && null != obj.getEffectEndTime()){
                sql.append(" AND #{effectStartTime}<=").append(baseAlias).append(".effect_time").append(" AND ").append("#{effectEndTime}>=").append(baseAlias).append(".effect_time");
            }
        }
    }

    /**
     * 服务于计算消耗、成本
     * @param sql
     * @param baseAlias
     * @param code
     * @param companyId
     */
    public void whereForgetMoney(StringBuffer sql,
                                 String baseAlias,
                                 @Param(value = "code") String code,
                                 @Param(value = "companyId") Integer companyId){
        if(!StringUtils.isEmpty(code)){
            sql.append(" AND ").append(baseAlias).append(".product_code=#{code}");
        }
        if(null != companyId){
            sql.append(" AND ").append(baseAlias).append(".company_id=#{companyId}");
        }
    }

    /**
     * 回显的list的where
     * @param sql
     * @param baseAlias
     * @param obj
     */
    public void whereConsumeListAll(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsConsume obj){
        sql.append(" AND ").append(baseAlias).append(".status <>2 OR ").append(baseAlias).append(".status IS NULL");
        if(!StringUtils.isEmpty(obj.getProductCode())){
            sql.append(" AND (").append(baseAlias).append(".product_code=#{productCode} OR ").append(baseAlias).append(".product_code IS NULL)");
        }
        if(null != obj.getCompanyId()){
            sql.append(" AND (").append(baseAlias).append(".company_id=#{companyId} OR ").append(baseAlias).append(".company_id IS NULL)");
        }
        if(null != obj.getCuserId()){
            sql.append(" AND (").append(baseAlias).append(".cuser_id=#{cuserId} OR ").append(baseAlias).append(".cuser_id IS NULL)");
        }
        if(null != obj.getInterfaceId()){
            sql.append(" AND ").append("inter").append(".id=#{interfaceId}");
        }
        if(null != obj.getFeeFlag()){
            sql.append(" AND ").append(baseAlias).append(".fee_flag=#{feeFlag}");
        }
        String interfaceIds = obj.getInterfaceIds();
        if(!StringUtils.isEmpty(interfaceIds)){
            String ids = interfaceIds.substring(0, interfaceIds.length() - 1);
            //写死的 接口表别名
            sql.append(" AND ").append("inter").append(".id IN(").append(ids).append(")");
        }
        if(null != obj.getEffectStartTime() && null != obj.getEffectEndTime()){
            sql.append(" AND (")
                    .append("(#{effectStartTime}>=").append(baseAlias).append(".effect_start_time AND #{effectStartTime} <=").append(baseAlias).append(".effect_end_time)")
                    .append("OR (#{effectEndTime}>=").append(baseAlias).append(".effect_start_time AND #{effectEndTime} <=").append(baseAlias).append(".effect_end_time)")
                    .append("OR (#{effectStartTime}<=").append(baseAlias).append(".effect_start_time AND #{effectEndTime}>= ").append(baseAlias).append(".effect_end_time)")
                    .append(")");
        }
    }

    /**
     * For Inter
     * @param sql
     * @param obj
     */
    public void whereConsumeListForInter(StringBuffer sql, @Param(value = "obj") ParamsConsume obj){
        if(!StringUtils.isEmpty(obj.getProductCode())){
            sql.append(" AND tp.product_code=#{productCode}");
        }
        if(null != obj.getCompanyId()){
            sql.append(" AND user.company_id=#{companyId}");
        }
        if(null != obj.getCuserId()){
            sql.append(" AND user.id=#{cuserId}");
        }
    }

    /**
     * base表的关联条件
     * @param sql
     * @param baseAlias
     * @param obj
     */
    public void whereConsumeListAllForCurr(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsConsume obj){
        sql.append(" AND ").append(baseAlias).append(".status <>2 ");
        if(!StringUtils.isEmpty(obj.getProductCode())){
            sql.append(" AND ").append(baseAlias).append(".product_code=#{productCode}");
        }
        if(null != obj.getCompanyId()){
            sql.append(" AND ").append(baseAlias).append(".company_id=#{companyId}");
        }
        if(null != obj.getCuserId()){
            sql.append(" AND ").append(baseAlias).append(".cuser_id=#{cuserId}");
        }
    }

    /**
     * forAll
     * @param sql
     * @param baseAlias
     * @param obj
     */
    public void whereConsumeListAllForAll(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsConsume obj){
        String interfaceIds = obj.getInterfaceIds();
        if(!StringUtils.isEmpty(interfaceIds)){
            String ids = interfaceIds.substring(0, interfaceIds.length() - 1);
            //写死的 接口表别名
            sql.append(" AND ").append(baseAlias).append(".id IN(").append(ids).append(")");
        }
    }
}
